1 using UnityEngine;
2 using
System.Collections;
3 using
System.Collections.Generic;
4
5
6 public
class GoSplineStraightLineSolver : AbstractGoSplineSolver
7 {
8     
private Dictionary<int, float> _segmentStartLocations;
9     
private Dictionary<int, float> _segmentDistances;
10     
private int _currentSegment;
11
12     
13     
public GoSplineStraightLineSolver( List<Vector3> nodes )
14     {
15         _nodes = nodes;
16     }
17     
18
19     
#region AbstractGoSplineSolver
20     
21     
public override void buildPath()
22     {
23         
// we need at least 3 nodes (more than 1 segment) to bother with building
24         
if( _nodes.Count < 3 )
25             
return;
26         
27         
// we dont care about the first node for distances because they are always t:0 and len:0 and we dont need the first or last for locations
28         _segmentStartLocations =
new Dictionary<int, float>( _nodes.Count - 2 );
29         _segmentDistances =
new Dictionary<int, float>( _nodes.Count - 1 );
30
31         
for( var i = 0; i < _nodes.Count - 1; i++ )
32         {
33             
// calculate the distance to the next node
34             
var distance = Vector3.Distance( _nodes[i], _nodes[i + 1] );
35             _segmentDistances.Add( i, distance );
36             _pathLength += distance;
37         }
38         
39
40         
// now that we have the total length we can loop back through and calculate the segmentStartLocations
41         
var accruedRouteLength = 0f;
42         
for( var i = 0; i < _segmentDistances.Count - 1; i++ )
43         {
44             accruedRouteLength += _segmentDistances[i];
45             _segmentStartLocations.Add( i +
1, accruedRouteLength / _pathLength );
46         }
47     }
48     
49     
50     
public override void closePath()
51     {
52         
// add a node to close the route if necessary
53         
if( _nodes[0] != _nodes[_nodes.Count - 1] )
54             _nodes.Add( _nodes[
0] );
55     }
56     
57     
58     
public override Vector3 getPoint( float t )
59     {
60         
return getPointOnPath( t );
61     }
62     
63     
64     
public override Vector3 getPointOnPath( float t )
65     {
66         
// we need at least 3 nodes (more than 1 segment) to bother using the look up tables. else we just lerp directly from
67         
// node 1 to node 2
68         
if( _nodes.Count < 3 )
69             
return Vector3.Lerp( _nodes[0], _nodes[1], t );
70         
71         
72         
// which segment are we on?
73         _currentSegment =
0;
74         
foreach( var info in _segmentStartLocations )
75         {
76             
if( info.Value < t )
77             {
78                 _currentSegment = info.Key;
79                 
continue;
80             }
81             
82             
break;
83         }
84         
85         
// now we need to know the total distance travelled in all previous segments so we can subtract it from the total
86         
// travelled to get exactly how far along the current segment we are
87         
var totalDistanceTravelled = t * _pathLength;
88         
var i = _currentSegment - 1; // we want all the previous segment lengths
89         
while( i >= 0 )
90         {
91             totalDistanceTravelled -= _segmentDistances[i];
92             --i;
93         }
94         
95         
return Vector3.Lerp( _nodes[_currentSegment], _nodes[_currentSegment + 1], totalDistanceTravelled / _segmentDistances[_currentSegment] );
96     }
97     
98     
#endregion
99
100 }



Trò chơi Angry Birds trong UNITY Engine 31.650 lượt xem

Gõ tìm kiếm nhanh...